package com.woldier.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.woldier.common.BaseContext;
import com.woldier.common.R;
import com.woldier.dto.UserDto;
import com.woldier.entity.User;
import com.woldier.service.UserService;
import com.woldier.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 发送手机短信验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user){
        //获取手机号
        String phone = user.getPhone();

        if(StringUtils.isNotEmpty(phone)){
            //生成随机的4位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code={}",code);

            //调用阿里云提供的短信服务API完成发送短信
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);

            //需要将生成的验证码保存到Session
//            session.setAttribute(phone,code);
            //v1.1存入redis key 用户id 值 code  过期时间3min
            redisTemplate.opsForValue().setIfAbsent(user.getPhone(),code,3, TimeUnit.MINUTES);

            return R.success("手机验证码短信发送成功");
        }

        return R.error("短信发送失败");
    }

    @PostMapping("login")
    public R login(@RequestBody UserDto user, HttpSession session){
        /*验证码对比 这里只是写出的相应的逻辑但是前端没有传值过来比对*/
        /*redis*/
        String code = (String) redisTemplate.opsForValue().get(user.getPhone());

        /*比对逻辑*/
        /*to do-*/
        log.info("从redis中取出的验证码{}=============user的验证码{}",code,user.getCode());
        /*比对不成功 直接返回*/
        if((code==null|code=="")||!code.equals(user.getCode()))
            return R.error("验证码不匹配");
        /*比对成功删除相关数据继续执行*/
        redisTemplate.delete(user.getPhone());

        /*查询数据库中是否有相应数据 有则直接登录,无则插入*/
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(User::getPhone,user.getPhone());
        User user1 = userService.getOne(lambdaQueryWrapper);


        /*保存session_id*/
        Long id = null;
        User resUser = null;
        if(user1==null){
            /*无数据 则插入*/
            userService.save(user);
            /*将插入后返回的id值设置到session中*/
           id = user.getId();
           resUser = user;

        }else {
            /*用户已再表中*/
            id = user1.getId();
            resUser = user1;
        }
        session.setAttribute("user",id);

        return R.success(resUser);

    }
}
